package com.smartHire.controller.Business;

import com.smartHire.domain.Goods;
import com.smartHire.domain.Order;
import com.smartHire.domain.Order_goods;
import com.smartHire.domain.User;
import com.smartHire.service.GoodsService;
import com.smartHire.service.OrderService;
import com.smartHire.service.Order_goodsService;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RefundServlet", value = "/RefundServlet")
public class RefundServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String order_id = request.getParameter("order_id");
        String u_Id = request.getParameter("u_Id");
        Float money = Float.parseFloat(request.getParameter("money"));
        int good_id= Integer.parseInt(request.getParameter("good_id"));
        int number= Integer.parseInt(request.getParameter("number"));

        System.out.println(order_id);
        System.out.println(u_Id);
        System.out.println(money);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            OrderService orderService = (OrderService) ctx.getBean("orderService");
            UserService userService = (UserService) ctx.getBean("userService");
            GoodsService goodsService =(GoodsService) ctx.getBean("goodsService");

            Order order = new Order();
            User user = new User();
            Goods goods= new Goods();

            order.setOrder_Id(order_id);
            order = orderService.queryOrderByOrder_Id(order);

            user.setU_Id(u_Id);
            user = userService.queryUserById(user);

            goods.setGood_id(good_id);
            goods = goodsService.queryGoodsByID(goods);

            order.setStatus(6);
            System.out.println(orderService.updateOrderStatus(order)==1);
            if(orderService.updateOrderStatus(order)==1)
            {
               money=money+user.getBalance();
               user.setBalance(money);

               if(userService.updateBalance(user)==1){
                   number = goods.getInventory()+number;
                   goods.setInventory(number);
                   if(goodsService.updateGoodsInventory(goods)==1){
                       response.getWriter().write("OK");
                   }
               }
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
